package cn.xf.springboot;

import cn.xf.User;
import cn.xf.Users;
import cn.xf.redis.RedisService;
import cn.xf.springboot.jpa.UserDao;
import cn.xf.springboot.mapper.UserMapper;
import cn.xf.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class HelloController {

    @RequestMapping("/index")
    public String index(Map<String,Object> map){
        map.put("name","美丽的天使...");
        return "index";
    }

    @Autowired
    private UserService userService;

    @RequestMapping("/createUser")
    public String createUser(Map<String,Object> map){
        userService.createUser("xiaofu",28);
        map.put("name","美丽的天使...");
        return "index";
    }

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/findByName")
    public String findByName(Map<String,Object> map,@RequestParam("name") String name){
        User user = userMapper.findByName(name);
        map.put("name",user.getName());
        return "index";
    }

    @RequestMapping("/insert")
    public String insert(Map<String,Object> map,@RequestParam("name") String name, @RequestParam("age")Integer age){
        userMapper.insert(name,age);
        System.out.println(name+age);
        map.put("name",name);
        return "index";
    }

    @Autowired
    private UserDao userDao;

    @RequestMapping("/findOne")
    public String findOne(Map<String,Object> map,@RequestParam("id") Integer id){
        Users user = userDao.findOne(id);
        map.put("name",user.getName());
        return "index";
    }

    @Autowired
    private RedisService redisService;

    @ResponseBody
    @RequestMapping("/setString")
    public String setString(String key,String value){
        redisService.setObject(key,value);
        String val = redisService.getString(key);
        return val;
    }

}
